//
//  HWRegisterViewController.m
//  HappyWeekend
//
//  Created by lanou on 16/1/14.
//  Copyright © 2016年 com.mjykj.www. All rights reserved.
//

#import "HWRegisterViewController.h"
#import "HWLoginAndRegisterViewController.h"

@interface HWRegisterViewController ()

@property (nonatomic, strong) UITextField *phoneTextField;

@property (nonatomic, strong) NSString *string;

@property (nonatomic, strong) HWRegisterView *registerView;
@end

@implementation HWRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:235.0f / 255.0f green:235.0f / 255.0f blue:241.0f / 255.0f alpha:1];
    [self addView];
}

- (void)addView{
    self.navigationItem.title = @"注册";
    _registerView = [[HWRegisterView alloc] initWithFrame:self.view.bounds];
    
    _registerView.nameView.userLabel.text = @"昵称:";
    _registerView.nameView.userText.placeholder = @"请输入昵称";
    
    _registerView.emailView.userLabel.text = @"邮箱:";
    _registerView.emailView.userText.placeholder = @"请输出邮箱";
    
    _registerView.passwordView.userLabel.text = @"密码:";
    _registerView.passwordView.userText.placeholder = @"请输入密码";
    _registerView.passwordView.userText.secureTextEntry = YES;
    
    [_registerView.finishButton setTitle:@"完成" forState:(UIControlStateNormal)];
    [_registerView.finishButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    
    [_registerView.finishButton addTarget:self action:@selector(finishButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_registerView];
}

- (void)finishButton:(UIButton *)sender{
    NSString *userName =  _registerView.nameView.userText.text;
    NSString *newName = [userName stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    NSLog(@"newName = %@",newName);
    NSString *email =  _registerView.emailView.userText.text;
    NSString *password = _registerView.passwordView.userText.text;
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    NSString *newEmail = [email stringByReplacingOccurrencesOfString:@"@" withString:@"%40"];
    parDic[@"auth"] = @"";
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"24538A91-BDBB-4437-AEE0-0E24D8BF0D58";
    parDic[@"gender"] = @"2";
    parDic[@"passwd"] = password;
    parDic[@"version"] = @"3.0.6";
    parDic[@"email"] = newEmail;
    parDic[@"uname"] = newName;
    [HWRequestManager requestWithUrl:@"http://api2.pianke.me/user/reg" parmDic:parDic method:POST1 succeedWithData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        if (dic[@"data"][@"uname"] != nil) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
           // NSString *title = dic[@"data"][@"msg"];
        }
    } failedWithError:^(NSError *connectionError) {
        
    }];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
